package com.example.actearly_revised;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import android.view.inputmethod.InputMethodManager;

import com.example.actearly_revised.JournalObject.JNode;
import com.example.actearly_revised.R;

public class NotesPage extends Activity {

	TextView NotesMessage;
	EditText Note;
	Button Save;
	Button Mile;
	int profile_number;
	SQLogic entry;
	static int currentMilestone;
	JournalObject previous_note;
	JNode journals;
	TabHomePage parent;
	User currUser;
	
	ImageButton leftButton;
	ImageButton rightButton;
	TextView currentAge;
	
	int ageCounter;
	int[] ageID;
	String[] ageGroups;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_notes_page);

		//font change
		Typeface customFont = Typeface.createFromAsset(getAssets(), "fonts/futuram.ttf");
		TextView Text = (TextView) findViewById(R.id.NotesLabel);
		EditText Text1 = (EditText) findViewById(R.id.txtNotes);

		Text1.setTypeface(customFont);
		Text.setTypeface(customFont);

		parent = (TabHomePage) this.getParent();
		this.currentMilestone = parent.currentmilestone;
		this.profile_number = parent.profile_number;
		
		leftButton = (ImageButton) findViewById(R.id.leftButton);
		rightButton = (ImageButton) findViewById(R.id.rightButton);
		currentAge = (TextView) findViewById(R.id.currentAge);
		
		
		currentAge.setTypeface(customFont);

		
		ageGroups= getResources().getStringArray(R.array.milestoneAges);
		ageID = new int[] {
				2,4,6,9,12,18,24,36,48,60
		};
		ageCounter = contains(this.currentMilestone,ageID);
		currentAge.setText(ageGroups[ageCounter]);

		entry = new SQLogic(NotesPage.this);
		entry.open();
		currUser = entry.getUserData(profile_number);
		previous_note = entry.RetrieveJournalEntry(profile_number);
		entry.close();


		String displayAge = ageGroups[ageCounter].replaceAll("years", "year");
		displayAge = displayAge.replaceAll("months", "month");
		NotesMessage = (TextView) findViewById(R.id.NotesLabel);
		NotesMessage.setText("Enter a note about " + currUser.childname + "\'s " + 
				displayAge 
				+" milestones that you would like to discuss with your doctor: ");

		Note = (EditText) findViewById(R.id.txtNotes);
		Note.requestFocus();

		System.out.println("the querry returned: " + previous_note.count() + " rows");
		System.out.println("the current milestone is " + currentMilestone);
		System.out.println("the current profile number is " + profile_number);

		Note.setText("");
		if(previous_note.count() > 0){
			journals = previous_note.get(Integer.toString(currentMilestone));
			if(journals != null)
				Note.setText(journals.Text);
		}

		Save = (Button) findViewById(R.id.bttnSave);
		Save.setTypeface(customFont);

		Mile = (Button) findViewById(R.id.bttnMileStones);
		Mile.setTypeface(customFont);
		
		Mile.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				TabHomePage tabhost;
				tabhost = (TabHomePage) NotesPage.this.getParent();
				tabhost.switchTab(0);
				//clear the edittext note when person switches back to tracker
				Note.setText("");
				
			}
		});
		
        leftButton.setOnClickListener(new View.OnClickListener() {
        	
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (ageCounter>0)
					ageCounter--;
				currentAge.setText(ageGroups[ageCounter]);
				NotesPage.currentMilestone = ageID[ageCounter];
				parent.currentmilestone = NotesPage.currentMilestone;
				String displayAge = ageGroups[ageCounter].replaceAll("years", "year");
				displayAge = displayAge.replaceAll("months", "month");
				NotesMessage.setText("Enter a note about " + currUser.childname + "\'s " + 
						displayAge 
						+" milestones that you would like to discuss with your doctor: ");
				
				entry.open();
				User cUser = entry.getUserData(profile_number);
				previous_note = entry.RetrieveJournalEntry(profile_number);
				entry.close();

				Note.setText(""); // clear previous note
				if(previous_note.count() > 0){
					journals = previous_note.get(Integer.toString(currentMilestone));
					if(journals != null)
						Note.setText(journals.Text);
				}
			}
		});
        
        rightButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (ageCounter<ageGroups.length-1)
					ageCounter++;
				currentAge.setText(ageGroups[ageCounter]);
				NotesPage.currentMilestone = ageID[ageCounter];
				parent.currentmilestone = NotesPage.currentMilestone;
				String displayAge = ageGroups[ageCounter].replaceAll("years", "year");
				displayAge = displayAge.replaceAll("months", "month");
				NotesMessage.setText("Enter a note about " + currUser.childname + "\'s " + 
						displayAge 
						+" milestones that you would like to discuss with your doctor: ");
				
				entry.open();
				User cUser = entry.getUserData(profile_number);
				previous_note = entry.RetrieveJournalEntry(profile_number);
				entry.close();

				Note.setText(""); // clear previous note
				if(previous_note.count() > 0){
					journals = previous_note.get(Integer.toString(currentMilestone));
					if(journals != null)
						Note.setText(journals.Text);
				}

			}
		});
		
		
		Save.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				entry.open();
				entry.UpdateJournal(currentMilestone, Note.getText().toString(), profile_number);
				entry.close();
				InputMethodManager imm = (InputMethodManager)getSystemService(
						Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(Save.getWindowToken(), 0);
				Context context = getApplicationContext();

				CharSequence text = "Note Saved!";
				int duration = Toast.LENGTH_SHORT;

				Toast toast = Toast.makeText(context, text, duration);
				toast.setGravity(Gravity.CENTER, 0, 150);
				toast.show();
			}
		});
	}

	@Override
	protected void onResume(){
		super.onResume();

		this.profile_number = parent.profile_number;
		this.currentMilestone = parent.currentmilestone;
		ageCounter = contains(this.currentMilestone,ageID);
		currentAge.setText(ageGroups[ageCounter]);

		entry.open();
		User cUser = entry.getUserData(profile_number);
		previous_note = entry.RetrieveJournalEntry(profile_number);
		entry.close();

		NotesMessage = (TextView) findViewById(R.id.NotesLabel);
		String displayAge = ageGroups[ageCounter].replaceAll("years", "year");
		displayAge = displayAge.replaceAll("months", "month");
		NotesMessage.setText("Enter a note about " + cUser.childname + "\'s " + displayAge +
				" milestones that you would like to discuss with your doctor: ");

		System.out.println("the querry returned: " + previous_note.count() + " rows");
		System.out.println("the current milestone is " + currentMilestone);
		System.out.println("the current profile number is " + profile_number);

		Note.setText(""); // clear previous note
		if(previous_note.count() > 0){
			journals = previous_note.get(Integer.toString(currentMilestone));
			if(journals != null)
				Note.setText(journals.Text);
		}
	}

	// save on pause and stop as well as the save button
	@Override
	protected void onPause(){  
		super.onPause();
		entry.open();
		entry.UpdateJournal(currentMilestone, Note.getText().toString(), profile_number);
		entry.close();
	} 

	@Override
	protected void onStop(){
		super.onStop();
		entry.open();
		entry.UpdateJournal(currentMilestone, Note.getText().toString(), profile_number);
		entry.close();
	}  

	// returns index of wanted int
	private int contains(int charac, int[] total){
		for(int i = 0; i < total.length; i++){
			if(total[i] == charac){
				return i;
			}
		}
		return -1;
	}
}
